<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Repository\ProductRepository;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/app")
 */
class ProductController extends BaseController4Restful
{
    /**
     * @Route("/product/list", name="AppIndexProductList")
     */
    public function AppIndexProductList(ProductRepository $repository)
    {
//        if(!$page = $this->getParamRequired("page")){$paeg = 1;}

        $page = $this->getParamRequired("page") ?: 1;
//        $user = $this->getUser();
//        if (!$user) {return $this->json_out(200,"获取成功",[]);}
        $qb = $repository->createQueryBuilder("A");
        $qb->select("A");
        $qb->where("A.status > 199");
        //todo 这里需要排除一下个人自己发布的订单
        $qb->orderBy("A.isIndex", "DESC");

        //地理位置排序
        if (!$lat = $this->getParamRequired("lat")) {
            $lat = 0;
        }
        if (!$lon = $this->getParamRequired("lon")) {
            $lon = 0;
        }
        if ($lat * $lon) {
            $qb->innerJoin("A.shop", "B");
//            $qb->addSelect("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon)) AS distance");
            $qb->orderBy("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon))", "ASC");
//            $qb->orderBy("distance","ASC");
        }
        $qb->addOrderBy("A.id", "DESC");
        $limit = 10;
        $qb->setMaxResults($limit ?? 10);
        $qb->setFirstResult(($page - 1) * $limit);
        $list = $qb->getQuery()->execute();
//        dd($list);
        return $this->json_out(200, "OK", $list);
    }

    /**
     * @Route("/products/lists", name="appProductsLists")
     */
    public function appProductsLists(ProductRepository $repository)
    {
        $page = $this->getParamRequired("page") ?: 1;
        $orderby = $this->getParamRequired("orderby") ?: 0;

        $qb = $repository->createQueryBuilder("A");
        $qb->select("A as product");
        $qb->innerJoin("A.shop", "B");
        $tag = $this->getParamRequired("tag") ?: 0;
        if ($tag) {
            $qb->innerJoin("B.tags", "T", "with", "T.id = " . (int)$tag);
        }

        $qb->where("A.status > 199");
        //todo 这里需要排除一下个人自己发布的订单

        //筛选行业
        $cat1 = $this->getParamRequired("cat1") ?: 0;
        $cat2 = $this->getParamRequired("cat2") ?: 0;
        if ($cat2) {
            $qb->andWhere("A.cat2 = " . (int)$cat2);
        } else {
            if ($cat1) {
                $qb->andWhere("A.cat1 = " . (int)$cat1);
            }
        }


        switch ($orderby) {
            case "near":
                //地理位置排序
                if (!$lat = $this->getParamRequired("lat")) {
                    $lat = 0;
                }
                if (!$lon = $this->getParamRequired("lon")) {
                    $lon = 0;
                }
                if ($lat * $lon) {
                    $qb->addSelect("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon)) AS distance");
//            $qb->orderBy("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon))","ASC");
                    $qb->orderBy("distance", "ASC");
                }
                break;
            case "price";
                $qb->orderBy("A.price","ASC");
                break;
            default:
        }

        //关键词检索
        if($keywords = $this->getParamRequired("keywords")){
            $qb->andWhere(
                $qb->expr()->like("A.name","'%{$keywords}%'")
            );
        }
        
        $limit = 10;
        $qb->setMaxResults($limit ?? 10);
        $qb->setFirstResult(($page - 1) * $limit);
        $list = $qb->getQuery()->execute();
        $this->setGroups("product","tags","tag");
        return $this->json_out(200, "OK", $list);
    }


    /**
     * @Route("/shop/products", name="appGetShopProductsLists")
     */
    public function appGetShopProductsLists(ProductRepository $repository)
    {
        if(!$id = $this->getParamRequired("id")){return $this->json_out(0,"参数缺失");}
        if(!$page = $this->getParamRequired("page")){$page = 1;}
        $qb = $repository->createQueryBuilder("A");
        $qb->select("A as product");
        $qb->innerJoin("A.shop", "B");
        $qb->where("A.status > 199");
        $qb->andWhere("B.id = ".$id);
        $qb->orderBy("A.id","DESC");
        $qb->setFirstResult($page * 10 -10)->setMaxResults(10);
        $data = $qb->getQuery()->execute();
        return $this->json_out(200, "OK", $data);
    }
}
